<!DOCTYPE html>
<html lang="zh">

<head>
	<meta charset="UTF-8">
	<meta content="width=device-width, initial-scale=1.0" name="viewport">
	<title>admin</title>
	<link href="https://cdn.staticfile.org/twitter-bootstrap/5.1.3/css/bootstrap.min.css" rel="stylesheet" />
	<link href="https://cdn.staticfile.org/bootswatch/5.1.3/darkly/bootstrap.min.css"
		media="(prefers-color-scheme: dark)" rel="stylesheet" />
	<style>
		body,
		input,
		textarea {
			font-family: "Microsoft JhengHei", "Meiryo", "Malgun Gothic", "Dotum", "MS PGothic", "PMingLiu", sans-serif;
		}

		body {
			background-color: #888;
		}

		@media (prefers-color-scheme: dark) {
			body {
				background: #222;
			}
		}
	</style>
</head>

<body>

	<div class="container my-4">
		<div id="return_msg"></div>
		<div class="card shadow mb-3" id="form1">
			<div class="card-header text-center">修改历史纪录</div>
			<div class="card-body">
				<div class="form-group">
					<label for="login_cid">雀庄编号</label>
					<input class="form-control" id="login_cid" placeholder="數字" type="number">
				</div>
				<div class="form-group">
					<label for="login_passwd">密碼</label>
					<input class="form-control" id="login_passwd" placeholder="密碼" type="password">
				</div>
				<div class="form-group">
					<label for="data_page">第几页</label>
					<input class="form-control" id="data_page" type="number" value="1">
					<small>1=最后50条，2=最后(51~100)条，以此类推</small>
				</div>
			</div>
			<div class="card-footer">
				<div class="d-grid gap-2 col-6 mx-auto">
					<input class="btn btn-primary btn-block" id="login_button" onclick="read_log()" type="button"
						value="读取纪录" />
				</div>

			</div>
		</div>
		<div class="card">
			<div class="card-header text-center">
				<button type="button" class="btn btn-primary" onclick="post_log(1)">提交表格的修改</button>
				<button type="button" class="btn btn-primary" onclick="post_log(2)">提交文本的修改</button>
				<br />日期改成0，则<kbd>删除这条记录</kbd>
				<br />数据的先后是看编号从小到大，<kbd>不看时间</kbd>
				<br />按住shift+滚轮 左右滑动表格，手机直接左右滑动</kbd>
				<br />这里的数据不会校验错误，请谨慎操作
				<div class="input-group">
					<span class="input-group-text">文本形式</span>
					<textarea class="form-control" id="logs_text"></textarea>
				</div>
			</div>
			<div class="card-body table-responsive">
				<table class="table table-sm table-bordered table-striped table-hover">
					<thead>
						<tr>
							<th scope="col">编号</th>
							<th scope="col">日期</th>
							<th scope="col">1位手机</th>
							<th scope="col">点数</th>
							<th scope="col">2位手机</th>
							<th scope="col">点数</th>
							<th scope="col">3位手机</th>
							<th scope="col">点数</th>
							<th scope="col">4位手机</th>
							<th scope="col">点数</th>
						</tr>
					</thead>
					<tbody id="data_tbody"></tbody>
				</table>
			</div>
		</div>
	</div>
	<script src="https://direct.r-mj.com/cache/apitest.js"></script>
	<script crossorigin="anonymous" src="https://cdn.staticfile.org/jquery/3.6.0/jquery.min.js"></script>
	<script crossorigin="anonymous"
		src="https://cdn.staticfile.org/twitter-bootstrap/5.1.3/js/bootstrap.bundle.min.js"></script>
	<script>
		if (window.location.origin === "http://bot0.000.mk") {
			window.apiurl = "http://bot0.000.mk";
		} else {
			if (!window.hasOwnProperty("apiurl")) {
				window.apiurl = "https://cdn.r-mj.com";
			}
		}

		window.json_data = [];
		function now_time() {
			var now = new Date();
			return now.getFullYear() + "/"
				+ (now.getMonth() + 1) + "/"
				+ now.getDate() + " "
				+ now.getHours() + ":"
				+ now.getMinutes() + ":"
				+ now.getSeconds();
		}
		function display_post(text) {
			var now = now_time();
			var div = document.createElement("div");
			div.setAttribute("class", "alert alert-success alert-dismissible fade show shadow-sm");
			div.setAttribute("role", "alert");
			var pre = document.createElement('pre');
			pre.appendChild(document.createTextNode(now + " " + text));
			div.appendChild(pre);
			var btn = document.createElement("button");
			btn.setAttribute("class", "btn-close");
			btn.setAttribute("data-bs-dismiss", "alert");
			btn.setAttribute("aria-label", "Close");
			div.appendChild(btn);
			var tgt = document.getElementById("return_msg");
			if (tgt.childNodes.length === 0) {
				tgt.appendChild(div);
			} else {
				tgt.insertBefore(div, tgt.childNodes[0]);
				document.documentElement.scrollTop = 0;
			}
			document.documentElement.scrollTop = 0;
		}
		function gen_table() {
			var tbody = document.getElementById('data_tbody');
			tbody.innerText = '';
			var json = window.json_data;
			var text = '';
			for (let i = 0; i < json.length; i++) {
				var tr = document.createElement('tr');
				var td = document.createElement('td');
				td.innerText = json[i][0];
				tr.appendChild(td);
				text += '\n' + json[i][0];
				for (let j = 1; j < json[i].length; j++) {
					var td = document.createElement('td');
					td.innerText = json[i][j];
					td.setAttribute('contentEditable', 'true');
					tr.appendChild(td);
					text += '\t' + json[i][j];
				}
				tbody.appendChild(tr);
			}
			document.getElementById('logs_text').value = text.trim();
		}

		function read_log() {
			var cid = document.getElementById('login_cid').value;
			var pw = document.getElementById('login_passwd').value;
			var page = document.getElementById('data_page').value;
			fetch(window.apiurl + '/r/edit_log.php?type=read&cid='
				+ cid + '&pw=' + pw + '&page=' + page)
				.catch(e => alert('错误' + e))
				.then((response) => response.json())
				.then((json) => {
					if (json.hasOwnProperty("status")) {
						if (json['status'] === 200) {
							window.json_data = json['data'];
							gen_table();
						} else {
							alert(json['msg']);
						}
					} else { alert('错误'); }
				});
		}
		function post_log(type) {
			var tbody = document.getElementById('data_tbody');
			var count = tbody.childElementCount;
			var logs = '';
			if (type === 1) {
				for (let i = 0; i < count; i++) {
					logs += '\n' + tbody.children[i].innerText;
					// + '\t' + (tbody.children[i].children[3].innerText - 250 + 300)
					// + '\t' + (tbody.children[i].children[5].innerText - 250 + 100)
					// + '\t' + (tbody.children[i].children[7].innerText - 250 - 100)
					// + '\t' + (tbody.children[i].children[9].innerText - 250 - 300)
				}
			}
			if (type === 2) {
				logs = document.getElementById('logs_text').value;
			}
			let formdata = new FormData();
			formdata.append("cid", document.getElementById('login_cid').value);
			formdata.append("pw", document.getElementById('login_passwd').value);
			formdata.append("type", "edit");
			formdata.append("logs", logs.trim());
			fetch(window.apiurl + '/r/edit_log.php', {
				"method": "POST",
				"body": formdata
			})
				.catch(e => alert('错误' + e))
				.then((response) => response.json())
				.then((json) => {
					if (json.hasOwnProperty("msg")) {
						display_post(json['msg']);
					} else { alert('错误'); }
				});
		}
	</script>

</body>

</html>